package com.xnew.role.controller;

import com.xnew.role.bean.AuRole;
import com.xnew.role.service.AuRoleService;
import com.xnew.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/sysrole-api")
public class RoleController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private AuRoleService auRoleService;

	/**
	 * 查询角色列表
	 * @param input
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/querySysRoleList")
	public DataOutput querySysRoleList(@RequestBody DataInput input, HttpServletRequest request) {
		DataOutput out = DataOutput.success();
		try {
			List<AuRole> roleList = auRoleService.querySysRoleList(input, request);
			out = MybatisParam.getOutput(ConvertEntityUtil.convertListData(roleList));
		} catch (Exception e) {
			e.printStackTrace();
			out.setCode(-91001);
			out.setMsg("查询列表失败!");
		}
		return out;
	}

	/**
	 * 根据id查询角色信息
	 * @param input
	 * @return request
	 */
	@ResponseBody
	@RequestMapping("/getSysRoleByid")
	public DataOutput getSysRoleByid(@RequestBody DataInput input, HttpServletRequest request) {
		DataOutput out = DataOutput.success();
		if (
			input.getParam() == null ||
			input.getParam().get("arId") == null ||
			StringUtils.isEmpty(input.getParam().get("arId").toString())
		){
			out.setCode(-91001);
			out.setMsg("缺少必填字段!");
			return out;
		}
		try {
			AuRole role = auRoleService.selectByPrimaryKey(input.getParam().get("arId").toString());
			out.setData(ConvertEntityUtil.convertEntityData(role));
		} catch (Exception e) {
			logger.error(e.getMessage());
			out.setCode(-91001);
			out.setMsg("操作失败!");
		}
		return out;
	}

	/**
	 * 根据id删除角色信息
	 * @param input
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteSysRoleByid")
	public DataOutput deleteSysRoleByid(@RequestBody DataInput input, HttpServletRequest request) {
		DataOutput out = DataOutput.success();
		if (
			input.getParam() == null ||
			input.getParam().get("arId") == null ||
			StringUtils.isEmpty(input.getParam().get("arId").toString())
		){
			out.setCode(-91001);
			out.setMsg("缺少必填字段!");
			return out;
		}
		try {
			auRoleService.deleteSysRoleByid(input.getParam().get("arId").toString());
		} catch (Exception e) {
			logger.error(e.getMessage());
			out.setCode(-91001);
			out.setMsg("操作失败!");
		}
		return out;
	}

	/**
	 * 新增或者修改角色信息
	 * @param input
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addOrUpdateRole")
	public DataOutput addOrUpdateRole(@RequestBody DataInput input, HttpServletRequest request){
		DataOutput out = DataOutput.success();
		if (
			input.getParam() == null ||
			input.getParam().get("arName") == null ||
			StringUtils.isEmpty(input.getParam().get("arName").toString()) ||
			input.getParam() == null ||
			input.getParam().get("arName") == null ||
			StringUtils.isEmpty(input.getParam().get("arName").toString()) ||
			input.getParam().get("arRemark") == null ||
			StringUtils.isEmpty(input.getParam().get("arRemark").toString())
		){
			out.setCode(-91001);
			out.setMsg("缺少必填字段!");
			return out;
		}
		try{
			AuRole auRole = new AuRole();
			auRole =(AuRole) ConvertEntityUtil.convertEntityByParam(input.getParam(), auRole);
			auRoleService.addOrUpdateRole(auRole, request);
		} catch (Exception e) {
			logger.error(e.getMessage());
			out.setCode(-91001);
			out.setMsg("操作失败!");
		}
		return out;
	}

	/**
	 * 新增个人用户角色权限
	 * @param input
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveIndvRoleUser")
	public DataOutput saveIndvRoleUser(@RequestBody DataInput input, HttpServletRequest request) {
		DataOutput out = DataOutput.success();
		if (
			input.getParam() == null ||
			input.getParam().get("arId") == null ||
			StringUtils.isEmpty(input.getParam().get("arId").toString())
		){
			out.setCode(-91001);
			out.setMsg("缺少必填字段!");
			return out;
		}
		try {
			AuRole auRole = new AuRole();
			auRole =(AuRole) ConvertEntityUtil.convertEntityByParam(input.getParam(), auRole);
			auRoleService.saveIndvRoleUser(auRole, request);
		} catch (Exception e) {
			logger.error(e.getMessage());
			out.setCode(-91001);
			out.setMsg("操作失败!");
		}
		return out;
	}

	/**
	 * 新增机构用户角色权限
	 * @param input
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveOrgRoleUser")
	public DataOutput saveOrgRoleUser(@RequestBody DataInput input, HttpServletRequest request) {
		DataOutput out = DataOutput.success();
		if (
			input.getParam() == null ||
			input.getParam().get("arId") == null ||
			StringUtils.isEmpty(input.getParam().get("arId").toString())
		){
			out.setCode(-91001);
			out.setMsg("缺少必填字段!");
			return out;
		}
		try {
			AuRole auRole = new AuRole();
			auRole =(AuRole) ConvertEntityUtil.convertEntityByParam(input.getParam(), auRole);
			auRoleService.saveOrgRoleUser(auRole, request);
		} catch (Exception e) {
			logger.error(e.getMessage());
			out.setCode(-91001);
			out.setMsg("操作失败!");
		}
		return out;
	}


	/**
	 * 删除用户角色权限
	 * @param input
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteRoleUser")
	public DataOutput deleteRoleUser(@RequestBody DataInput input, HttpServletRequest request) {
		DataOutput out = DataOutput.success();
		if (
				input.getParam() == null ||
						input.getParam().get("arId") == null ||
						StringUtils.isEmpty(input.getParam().get("arId").toString()) ||
						StringUtils.isEmpty(input.getParam().get("auIds").toString())
		){
			out.setCode(-91001);
			out.setMsg("缺少必填字段!");
			return out;
		}
		try {
			auRoleService.deleteRoleUser(input.getParam().get("arId").toString(), input.getParam().get("auIds").toString());
		} catch (Exception e) {
			logger.error(e.getMessage());
			out.setCode(-91001);
			out.setMsg("操作失败!");
		}
		return out;
	}



}
